package ylsb.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.*;
import ylsb.service.*;
import ylsb.support.JsonResult;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("yiyongyuanze")
public class YiyongyuanzeController {
    @Resource
    YiyongyuanzeService ddService;
    @Resource
    YltprenyuanfenpeiService ddYltprenyuanfenpeiService;
    @Resource
    YltpXiangmuService ddYltpXiangmuService;
    @Resource
    YiyonghaocaiService ddYiyonghaocaiService;
    @Resource
    YiyongjieguoService ddYiyongjieguoService;
    @Resource
    Hc45Service ddHc45Service;
    @Resource
    HcjieguoService ddHcjieguoService;

    @RequestMapping("find_one")
    public JsonResult<?> findOne(
            @RequestParam("detailid") String ppdetailid,
            @RequestParam("pinzhong") String pppinzhong,
            @RequestParam("pinpai") String pppinpai,
            @RequestParam("xiangmuid") String ppxiangmuid,
            @RequestParam("userid") String ppuserid
    ){
        try {
            return JsonResult.getSuccessResult(ddService.findOne(pppinzhong,pppinpai,ppxiangmuid,ppuserid));
        }catch(Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("查询失败！"+e);
        }
    }
    @RequestMapping("save")
    public synchronized JsonResult<?> save(@RequestParam("id") List<String> ppdetailIds,
                                            @RequestParam("pinzhong") int ppPinzhong,
                                            @RequestParam("pinpai") String ppPinpai,
                                            @RequestParam("xiangmuid") String ppxiangmuid,
                                            @RequestParam("shiyongyuanzeid") String ppshiyongyuanzeid,
                                            @RequestParam("userid") String ppuserId){

        try {

            YltpXiangmu mmxiangmu=ddYltpXiangmuService.findOne(ppxiangmuid);
            if(mmxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("保存失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId,ppxiangmuid);
            if(listfenpei.get(0).getIssubmit()==1){
                return JsonResult.getErrorResult("保存失败，当前投票项已提交");
            }

            Yiyongyuanze mmYiyongyuanze=new Yiyongyuanze();
            Hcjieguo mmjieguo = new Hcjieguo();
            List<Map<String, Object>> mmpinpailist=ddHcjieguoService.mmPinpaiByPinzhongList(ppxiangmuid, ppuserId,ppPinzhong);
            int flag = 0;
            if(mmpinpailist!=null){//品牌内
               if(mmpinpailist.stream().noneMatch(x -> x.get("pinpai").toString().equals(ppPinpai))){
                   if (ddHcjieguoService.mmPinpaiByPinzhongList(ppxiangmuid, ppuserId,ppPinzhong).size() < 3 )flag=1;
               }else {
                   flag=1;
               }
            } else{
                flag=1;
            }
            if (flag==1 ) {
            for (String mmdetailId : ppdetailIds) {
                Yiyongyuanze mmyuanze=ddService.checkexistByUserId(mmdetailId,ppPinzhong, ppPinpai, ppuserId,ppxiangmuid);
                if (mmyuanze != null) {
                    String mmId = mmyuanze.getId();
                    mmYiyongyuanze.setId(mmId);
                    mmYiyongyuanze.setShiyongyuanzeid(ppshiyongyuanzeid);

                    Hcjieguo hcjieguo=ddHcjieguoService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, ppxiangmuid);
                    if (hcjieguo != null) {
                        String mmjieguoId = hcjieguo.getId();
                        mmjieguo.setId(mmjieguoId);
                        mmjieguo.setXiangmuid(ppxiangmuid);
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddHcjieguoService.modify(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }

                    } else {
                        mmjieguo.setId(UUID.randomUUID().toString());
                        mmjieguo.setDetailid(mmdetailId);
                        mmjieguo.setPinzhong(ppPinzhong);
                        mmjieguo.setPinpai(ppPinpai);
                        mmjieguo.setUserid(ppuserId);
                        mmjieguo.setXiangmuid(ppxiangmuid);
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddHcjieguoService.add(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }
                    }
                    if(ddService.modify(mmYiyongyuanze)!=true) {
                        return JsonResult.getErrorResult("保存失败");
                    }

                } else {
                    Hcjieguo hcjieguo=ddHcjieguoService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, ppxiangmuid);
                    if (hcjieguo != null) {
                        String mmjieguoId = hcjieguo.getId();
                        mmjieguo.setId(mmjieguoId);
                        mmjieguo.setXiangmuid(ppxiangmuid);
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddHcjieguoService.modify(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }

                    } else {
                        mmjieguo.setId(UUID.randomUUID().toString());
                        mmjieguo.setDetailid(mmdetailId);
                        mmjieguo.setPinzhong(ppPinzhong);
                        mmjieguo.setPinpai(ppPinpai);
                        mmjieguo.setUserid(ppuserId);
                        mmjieguo.setXiangmuid(ppxiangmuid);
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddHcjieguoService.add(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }
                }
                    mmYiyongyuanze.setId(UUID.randomUUID().toString());
                    mmYiyongyuanze.setDetailid(mmdetailId);
                    mmYiyongyuanze.setPinzhong(ppPinzhong);
                    mmYiyongyuanze.setPinpai(ppPinpai);
                    mmYiyongyuanze.setUserid(ppuserId);
                    mmYiyongyuanze.setXiangmuid(ppxiangmuid);
                    mmYiyongyuanze.setShiyongyuanzeid(ppshiyongyuanzeid);
                    mmYiyongyuanze.setCreatetime(new Date());
                    if(ddService.add(mmYiyongyuanze)!=true){
                        return JsonResult.getErrorResult("保存失败");
                    }
                }
            }
            } else {
            return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过3条。");
            }

            return  JsonResult.getSuccessResult("保存成功");
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("yiyongyuanze/save:error " + e.getMessage());
        }
    }
   /* @RequestMapping("save")
    public synchronized JsonResult<?> save( @RequestParam("detailid") String ppdetailId,
                                  @RequestParam("pinzhong") int ppPinzhong,
                                  @RequestParam("pinpai") String ppPinpai,
                                  @RequestParam("xiangmuid") String ppxiangmuid,
                                  @RequestParam("shiyongyuanzeid") String ppshiyongyuanzeid,
                                  @RequestParam("userid") String ppuserId){

        try {
            Yiyongyuanze mmYiyongyuanze=new Yiyongyuanze();
            Yiyonghaocai mmYihonghaocai=ddYiyonghaocaiService.findOne(ppdetailId);

            YltpXiangmu mmxiangmu=ddYltpXiangmuService.findOne(mmYihonghaocai.getXiangmuid());
            if(mmxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("保存失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId,mmYihonghaocai.getXiangmuid());
            if(listfenpei.get(0).getIssubmit()==1){
                return JsonResult.getErrorResult("保存失败，当前投票项已提交");
            }

            Yiyongjieguo mmjieguo=new Yiyongjieguo();
            if (ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId,ppxiangmuid) != null) {

                String mmId = ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId,ppxiangmuid).getId();
                mmYiyongyuanze.setId(mmId);
                mmYiyongyuanze.setShiyongyuanzeid(ppshiyongyuanzeid);

                    if (ddYiyongjieguoService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId,mmYihonghaocai.getXiangmuid()) != null) {
                        String mmjgId = ddYiyongjieguoService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId,mmYihonghaocai.getXiangmuid()).getId();
                        mmjieguo.setId(mmjgId);
                        mmjieguo.setXinghao(mmYihonghaocai.getXiangmuid());
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddYiyongjieguoService.modify(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }
                    }else {
                        mmjieguo.setId(UUID.randomUUID().toString());
                        mmjieguo.setDetailid(ppdetailId);
                        mmjieguo.setPinzhong(ppPinzhong);
                        mmjieguo.setPinpai(ppPinpai);
                        mmjieguo.setUserid(ppuserId);
                        mmjieguo.setXinghao(mmYihonghaocai.getXiangmuid());
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddYiyongjieguoService.add(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }
                    }


                return ddService.modify(mmYiyongyuanze)==true? JsonResult.getSuccessResult("保存成功")
                        : JsonResult.getErrorResult("保存失败");
            }else{

                if (ddYiyongjieguoService.checkCountByPinzhongandUserId(ppPinzhong, ppuserId,mmYihonghaocai.getXiangmuid()) < 3 ) {
                    if (ddYiyongjieguoService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId,mmYihonghaocai.getXiangmuid()) != null) {
                        String mmjgId = ddYiyongjieguoService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId,mmYihonghaocai.getXiangmuid()).getId();
                        mmjieguo.setId(mmjgId);
                        mmjieguo.setXinghao(mmYihonghaocai.getXiangmuid());
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddYiyongjieguoService.modify(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }
                    }else {
                        mmjieguo.setId(UUID.randomUUID().toString());
                        mmjieguo.setDetailid(ppdetailId);
                        mmjieguo.setPinzhong(ppPinzhong);
                        mmjieguo.setPinpai(ppPinpai);
                        mmjieguo.setUserid(ppuserId);
                        mmjieguo.setXinghao(mmYihonghaocai.getXiangmuid());
                        mmjieguo.setResult("1");
                        mmjieguo.setCreatetime(new Date());
                        if(ddYiyongjieguoService.add(mmjieguo) != true){
                            return JsonResult.getErrorResult("表决失败");
                        }
                    }
                } else {
                    return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过3条。");
                }

                mmYiyongyuanze.setId(UUID.randomUUID().toString());
                mmYiyongyuanze.setDetailid(ppdetailId);
                mmYiyongyuanze.setPinzhong(ppPinzhong);
                mmYiyongyuanze.setPinpai(ppPinpai);
                mmYiyongyuanze.setUserid(ppuserId);
                mmYiyongyuanze.setXiangmuid(ppxiangmuid);
                mmYiyongyuanze.setShiyongyuanzeid(ppshiyongyuanzeid);
                mmYiyongyuanze.setCreatetime(new Date());

                return ddService.add(mmYiyongyuanze)==true? JsonResult.getSuccessResult("保存成功")
                        : JsonResult.getErrorResult("保存失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("yiyongyuanze/save:error " + e.getMessage());
        }
    }*/

    @RequestMapping("jiancha")
    public JsonResult<?> jiancha( @RequestParam("detailid") String ppdetailId,
                                  @RequestParam("pinzhong") int ppPinzhong,
                                  @RequestParam("pinpai") String ppPinpai,
                                  @RequestParam("xiangmuid") String ppxiangmuid,
                                  @RequestParam("shiyongyuanzeid") String ppshiyongyuanzeid,
                                  @RequestParam("userid") String ppuserId){

        try {

            YltpXiangmu mmxiangmu=ddYltpXiangmuService.findOne(ppxiangmuid);
            if(mmxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("保存失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId,ppxiangmuid);
            if(listfenpei.get(0).getIssubmit()==1){
                return JsonResult.getErrorResult("保存失败，当前投票项已提交");
            }


            return JsonResult.getSuccessResult("成功");
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("yiyongyuanze/jiancha:error " + e.getMessage());
        }
    }
    /*@RequestMapping("jiancha")
    public JsonResult<?> jiancha( @RequestParam("detailid") String ppdetailId,
                               @RequestParam("pinzhong") int ppPinzhong,
                               @RequestParam("pinpai") String ppPinpai,
                               @RequestParam("xiangmuid") String ppxiangmuid,
                               @RequestParam("shiyongyuanzeid") String ppshiyongyuanzeid,
                               @RequestParam("userid") String ppuserId){

        try {
            Yiyongyuanze mmYiyongyuanze=new Yiyongyuanze();
            Yiyonghaocai mmYihonghaocai=ddYiyonghaocaiService.findOne(ppdetailId);

            YltpXiangmu mmxiangmu=ddYltpXiangmuService.findOne(mmYihonghaocai.getXiangmuid());
            if(mmxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("保存失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId,mmYihonghaocai.getXiangmuid());
            if(listfenpei.get(0).getIssubmit()==1){
                return JsonResult.getErrorResult("保存失败，当前投票项已提交");
            }
            List<Map<String, Object>> mmyibaoList=ddYiyonghaocaiService.findyibaoBypinzhongAndpinpai(ppxiangmuid,String.valueOf(ppPinzhong),ppPinpai);
            if(mmyibaoList.size()==0){
                if(ppshiyongyuanzeid.contains("1")){
                    return JsonResult.getErrorResult("当前品牌没有医保,确定选择此适用原则吗？");
                }
            }
            List<Map<String, Object>> mmGuochanList=ddYiyonghaocaiService.findGuochanBypinzhongAndpinpai(ppxiangmuid,String.valueOf(ppPinzhong),ppPinpai);
            if(mmGuochanList.size()==0){
                if(ppshiyongyuanzeid.contains("2")){
                    return JsonResult.getErrorResult("当前品牌没有国产,确定选择此适用原则吗？");
                }
            }
            List<Map<String, Object>> mmZhuanjiList=ddYiyonghaocaiService.findZuanjiBypinzhong(ppxiangmuid,String.valueOf(ppPinzhong),ppPinpai);
            if(mmZhuanjiList.size()==0){
                if(ppshiyongyuanzeid.contains("4")){
                    return JsonResult.getErrorResult("当前品牌没有专机,确定选择此适用原则吗？");
                }
            }

            return JsonResult.getSuccessResult("成功");
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("yiyongyuanze/jiancha:error " + e.getMessage());
        }
    }*/



}
